package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hrbxlh.util.PubMethods;
import com.hrbxlh.util.SessionValues;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.NoticeActionImpl;
import com.hrbxlh.util.PropertiesUtil;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ClassAction.java
* Description：消息action类
* History：
* Date           Author               Desc
* 2017.3.29		  sy                   建立
* -------------------------------------------------------
*/
@Controller
public class NoticeAction {

	@Autowired
	@Qualifier("noticeActionImpl")
	private NoticeActionImpl noticeActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	//日志信息
	Logger logger = Logger.getLogger(MemberAction.class.getName());
	
//	/**
//	 *
//	 * @return 查询通知设置。
//	 * @throws IOException
//	 */
//	@SuppressWarnings("unchecked")
//	@RequestMapping(value = "/getNotice", method = RequestMethod.GET)
//	public ModelAndView getNotice(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
//
//		//转换json格式工具
//		ObjectMapper mapper = new ObjectMapper();
//		//存放返回值
//		Map<String, Object> returnMap = new HashMap<String, Object>();
//		//返回字符串
//		String reJsonStr = "";
//		//存放页面
//		String pageName = "";
//		//存放结果值
//		Map<String,Object> resultMap = new HashMap<String,Object>();
//		//日志信息
//		Logger logger = Logger.getLogger(MemberAction.class.getName());
//		try {
//
//
//			//获取消息信息
//			Map<String, Object> resultAllMap = noticeActionImpl.getNotice(session);
//			//查询当前人的消息
//			List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
//			resultMap.put("noticeList", noticeList);
//			//返回值
//			resultMap.put("noticeMap", resultAllMap.get("noticeMap"));
//			resultMap.put("classList", resultAllMap.get("classList"));
//			resultMap.put("memberList", resultAllMap.get("memberList"));
//		    //人员列表页面名称
//			pageName = "pc/noticeSet";
//		} catch (Exception e) {
//			resultMap.put("errMsg", "跳转页面失败");
//			e.printStackTrace();
//			logger.error("消息设置异常", e);
//			pageName = "public/pageerror";
//		} finally {
//			reJsonStr = mapper.writeValueAsString(resultMap);
//			returnMap.put("result", reJsonStr);
//			System.out.println("返回的  :" + reJsonStr);
//		}
//		return new ModelAndView(pageName, returnMap);
//	}

	/**
	 *
	 * @return 查询通知设置。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getNotice", method = RequestMethod.POST)
	public void getNotice(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();

		try {
			//获取消息信息
			Map<String, Object> resultAllMap = noticeActionImpl.getNotice(session,request);
			resultMap.put("noticeMap", resultAllMap.get("noticeMap"));
			resultMap.put("classList", resultAllMap.get("classList"));
			resultMap.put("memberList", resultAllMap.get("memberList"));
			logger.info("查询通知设置的参数为："+jsonStr);

		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("查询通知设置异常", e);

		} finally {
			logger.info("查询通知设置的返回结果为："+resultMap);
			PubMethods.postMed(response,mapper,resultMap);
		}

	}
	
	/**
	 * @param request
	 * @param response
	 * @return  跳转到添加消息设置页面
	 * @throws IOException
	 *//*
	@RequestMapping(value = "/AddNoticeView", method = RequestMethod.GET)
	public ModelAndView AddNoticeView(String jsonStr,HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		System.out.println("添加消息设置页面");
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		
		try {
			
			Map<String, Object> resultAllMap = noticeActionImpl.getNotice(session);
			//返回值
			resultMap.put("noticeMap", resultAllMap.get("noticeMap"));
			resultMap.put("classList", resultAllMap.get("classList"));
			resultMap.put("memberList", resultAllMap.get("memberList"));
			resultMap.put("noticeClassList", resultAllMap.get("noticeClassList"));
			String[] interfaceNameArrUse = {"server_address","project_name"};
			String[] valueArrUse = PropertiesUtil.getUrl(interfaceNameArrUse);
			String urlUse = "";
			for(String value:valueArrUse){
				urlUse += value;
			}
			//返回图片地址
            String returnUrl= urlUse+"images/pc";
            resultMap.put("imageUrl", returnUrl);
           //查询当前人的消息
			List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加页面名称
			pageName = "pc/addNotice";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转消息页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}*/

	/**
	 * @param request
	 * @param response type添加1 修改2
	 * @return  跳转到添加或修改消息设置页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/AddNoticeView", method = RequestMethod.POST)
	public void AddNoticeView(String jsonStr,HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String, Object> resultAllMap = new HashMap<String, Object>();

		try {
			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap = mapper.readValue(jsonStr, paramMap.getClass());
			if(Integer.parseInt(String.valueOf(paramMap.get("type")))==1){
				resultAllMap = noticeActionImpl.getNoticeNoClass(session,request);
				resultMap.put("noticeMap", null);
			}if(Integer.parseInt(String.valueOf(paramMap.get("type")))==2){
				resultAllMap = noticeActionImpl.getNoticeByNoticeId(paramMap,session,request);
				resultMap.put("noticeMap", resultAllMap.get("noticeMap"));
			}
			//返回值
			resultMap.put("classList", resultAllMap.get("classList"));
			resultMap.put("memberList", resultAllMap.get("memberList"));
			resultMap.put("noticeClassList", resultAllMap.get("noticeClassList"));
			logger.info("  跳转到添加或修改消息设置页面的参数为："+jsonStr);
		} catch (Exception e) {
			resultMap.put("errMsg", " 跳转到添加或修改消息设置页面失败");
			e.printStackTrace();
			logger.error(" 跳转到添加或修改消息设置页面出现异常", e);

		} finally {
			logger.info("  跳转到添加或修改消息设置页面的返回结果为："+resultMap);
			PubMethods.postMed(response,mapper,resultMap);
		}
	}

	/**
	 * @param request
	 * @param response type 1 添加 2修改
	 * @return  消息设置。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addNoticeSet", method = RequestMethod.POST)
	public void addNoticeSet(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		///转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//成功失败标识
		String resultMark = "fail";
		String errorMsg = "";
		try {

			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//消息设置。
			Map<String,Object> returnMap = noticeActionImpl.addNoticeSet(paramMap,session,request);
			boolean resultFlag = Boolean.parseBoolean(String.valueOf(returnMap.get("returnFlag")));
			resultMark = (resultFlag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			resultMap.put("errorMsg", returnMap.get("msg"));
			logger.info("消息设置的参数为："+jsonStr);
		} catch (Exception e) {
			resultMap.put("errorMsg", "添加消息出现异常");
			e.printStackTrace();
			logger.error("添加消息出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			logger.info("消息设置返回的结果为："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}

	/**
	 * @description xzx 管理员设置信息提交(添加修改都走这个)
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addNotice", method = RequestMethod.POST)
	public void addNotice(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {
		noticeActionImpl.addNotice(jsonStr, request, response);
	}


	/**20171019sy新需求--删除消息
	 * @param request noticeId
	 * @param response
	 * @return  消息设置。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delNoticeSet", method = RequestMethod.POST)
	public void delNoticeSet(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//成功失败标识
		String resultMark = "fail";
		String errorMsg = "";
		try {

			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//消息设置。
			boolean resultFlag = noticeActionImpl.delNoticeSet(paramMap,session,request);
			resultMark = (resultFlag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				errorMsg = "删除消息失败";
			}
			resultMap.put("errorMsg",errorMsg );
			logger.info("删除消息的参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("errorMsg","删除消息出现异常" );
			logger.error("删除消息出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			logger.info("删除消息返回的结果为："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  跳转到帮助文档页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/adminHelpAction", method = RequestMethod.GET)
	public ModelAndView adminHelpAction(HttpServletRequest request, HttpSession session) throws IOException {
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			//查询当前人的消息
			/*List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);*/
			//判断当前登陆人的身份
			Map<String, Object> sessionMap = new SessionValues().getSessionValues(new String[] {"roleId"}, request, "userMsg");
			int roleId = Integer.parseInt(String.valueOf(sessionMap.get("roleId"))) ;
			if(roleId==3){
				//管理员帮助文档页面
				pageName = "pc/adminHelp";
			}if(roleId==2){
				//教师帮助文档页面
				pageName = "pc/teaHelp";
			}
			
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转到帮助文档页面出现异常", e);
			pageName = "error";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}

	/**
	 * @param request
	 * @param response
	 * @return  跳转到帮助文档页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/adminHelpAction", method = RequestMethod.POST)
	public void adminHelpAction(HttpServletRequest request, HttpSession session,HttpServletResponse response) throws IOException {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();

		try {
			//判断当前登陆人的身份
			Map<String, Object> sessionMap = new SessionValues().getSessionValues(new String[] {"roleId"}, request, "userMsg");
			int roleId = Integer.parseInt(String.valueOf(sessionMap.get("roleId"))) ;
			resultMap.put("roleId",roleId);
			/*if(roleId==3){
				//管理员帮助文档页面
				pageName = "pc/adminHelp";
			}if(roleId==2){
				//教师帮助文档页面
				pageName = "pc/teaHelp";
			}*/

		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转到帮助文档页面出现异常", e);

		} finally {
			PubMethods.postMed(response,mapper,resultMap);
		}

	}

	/**
	 *
	 * @return 查询消息通知集合（学生端--过期的消息也查）。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getNoticeListByTeaId", method = RequestMethod.POST)
	public void getNoticeListByTeaId(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();

		try {
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取消息信息
			Map<String, Object> resultAllMap = noticeActionImpl.getNoticeListByTeaId(preParamMap,session,request);
			//返回值
			resultMap.put("noticeListByStu", resultAllMap.get("noticeListByStu"));
			resultMap.put("totalPage", resultAllMap.get("totalPage"));
			resultMap.put("pageNow", preParamMap.get("pageNow"));
			logger.info("查询通知设置的参数为："+jsonStr);
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("查询通知设置异常", e);

		} finally {
			logger.info("查询通知设置的返回结果为："+resultMap);
			PubMethods.postMed(response,mapper,resultMap);
		}

	}

	/**
	 *
	 * @return 查询消息通知集合（教师端和管理员端--过期的消息也查）。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getNoticeListByAdminAndTeaId", method = RequestMethod.POST)
	public void getNoticeListByAdminAndTeaId(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {

		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();

		try {
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取消息信息
			Map<String, Object> resultAllMap = noticeActionImpl.getNoticeListByAdminAndTeaId(preParamMap,session,request);
			//返回值
			resultMap.put("adminAndTeaNoticeList", resultAllMap.get("adminAndTeaNoticeList"));
			resultMap.put("totalPage", resultAllMap.get("totalPage"));
			resultMap.put("pageNow", preParamMap.get("pageNow"));
			logger.info("查询通知设置的参数为："+jsonStr);
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("查询通知设置异常", e);

		} finally {
			logger.info("查询通知设置的返回结果为："+resultMap);
			PubMethods.postMed(response,mapper,resultMap);
		}

	}
}
